const express = require("express");
const path = require("path");
const fs = require("fs");
const busboy = require("busboy");
const os = require("os");
var router = express.Router();

/**,
 * @swagger
 * /file/upload:
 *    post:
 *      tags:
 *      - 文件上传
 *      summary: 通用文件上传api
 *      produces:
 *      - application/json
 *      requestBody:
 *          content:
 *           multipart/form-data:
 *             schema:
 *                type: object
 *                properties:
 *                  file:
 *                     type: string
 *                     format: binary
 *      responses:
 *        200:
 *              description: 成功
 *              content:
 *                  application/json:
 *                    schema:
 *                        type: object
 *                        properties:
 *                           data:
 *                             type: string
 *                           message:
 *                             type: string
 *                           code:
 *                             type: number
 *                             example: 200
 *                           success:
 *                              type: boolean
 *                              example: true
 *
 *        400:
 *          description: Invalid parameter supplied
 *        500:
 *          description: Unexpected error
 * */

router.post("/upload", function (req, res) {
  const bb = busboy({ headers: req.headers, defParamCharset: "utf-8" });

  bb.on("file", (name, file, info) => {
    const { filename } = info;
    const protocol = req.protocol; // 获取请求的协议，可能是 'http' 或 'https'
    const host = req.hostname; // 获取请求的
    const saveTo = path.join(os.homedir(), "data/images/", filename);
    file
      .pipe(fs.createWriteStream(saveTo))
      .on("finish", () => {
        res.send({
          code: 200,
          data: `${protocol}://${host}/images/${filename}`,
          message: "上传成功",
          success: true,
        });
      })
      .on("error", (err) => {
        console.error("文件写入错误:", err);
        res.status(500).send({
          code: 500,
          message: "文件写入错误",
          success: false,
        });
      });
  });

  bb.on("error", (err) => {
    console.error("文件上传错误:", err);
    res.status(500).send({
      code: 500,
      message: "文件上传错误",
      success: false,
    });
  });

  req.pipe(bb);
});

// /**,
//  * @swagger
//  * /file/batchUpload:
//  *    post:
//  *      tags:
//  *      - 文件上传
//  *      summary: 批量文件上传api
//  *      produces:
//  *      - application/json
//  *      requestBody:
//  *          content:
//  *           multipart/form-data:
//  *             schema:
//  *                type: object
//  *                properties:
//  *                  file:
//  *                     type: array
//  *                     items:
//  *                        type: string
//  *                        format: binary
//  *
//  *      responses:
//  *        200:
//  *          description: 成功
//  *        400:
//  *          description: 参数类型错误
//  *        500:
//  *          description: 服务器错误
//  * */
// router.post('/batchUpload', (req, res) => {
//     const bb = busboy({ headers: req.headers, defParamCharset: 'utf-8' })
//     bb.on('file', (name, file, info) => {
//         const { filename } = info
//         const saveTo = path.join(os.homedir(), `../data/images/${filename}`);
//         res.data = filename
//         file.pipe(fs.createWriteStream(saveTo));
//     });
//     bb.on('error', (err) => {
//         console.log(err)
//     })
//     bb.on('close', () => {
//         res.send({
//             code: 200,
//             data: `http://localhost/images/${res.data}`,
//             message: '上传成功'
//         });
//     });
//     req.pipe(bb);
//     return
// })

module.exports = router;
